var Bbsplate = {};

Bbsplate.init = function(){
	Bbsplate.initGrid();
}

Bbsplate.initGrid = function(){
	$('#plateListTable').datagrid({
		width:768,
		height:'auto',
		striped: true,
		url:context+'/manager/bbs!plateData.action',
		remoteSort: false,
		idField:'id',
		columns:[[
					{field:'code',title:'编号',width:100,align:'center'},
					{field:'title',title:'名称',width:140,align:'center'},
					{field:'userName',title:'添加人',width:120,align:'center'},
					{field:'description',title:'简介',width:256,align:'center',rowspan:2},
					{field:'id',title:'操作',width:119,align:'center',formatter:function(value,rec){
						return "<a href='#' onclick='Bbsplate.modPlate("+value+")' style='color:blue'>编辑</a> | <a href='#' onclick='Bbsplate.deletePlate("+value+")'  style='color:blue'>删除</a>";
					}}
				]],
		pagination:true,
		loadMsg:'正在加载数据,请等待...',
		rownumbers:true
	});
	var p = $('#plateListTable').datagrid('getPager');
	if (p){
		$(p).pagination({
			showPageList:false,
			beforePageText:'第',
			afterPageText:"页，共<font color='blue'> {pages} </font>页",
			displayMsg:"当前显示 <font color='red'>{from}</font> 到 <font color='red'>{to}</font> 条记录，共<font color='red'> {total} </font>条记录",
		});
	}
}

Bbsplate.modPlate = function(plateId){
	location.href = context+'/manager/bbs!modPlate.action?plateId='+plateId;
}

Bbsplate.deletePlate = function(plateId){
	$.messager.defaults={ok:"确定",cancel:"取消"};
	$.messager.confirm('', '确定删除吗？', function(r){
		if (r){
			$.ajax({
				type:"POST",
				url:context+'/manager/bbs!delPlate.action', 
				data:{
					'plateId':plateId
				},
				success:function(text){
					$('#plateListTable').datagrid('reload');
				}
			});
		}
	});
}

Bbsplate.initSelect = function(userId){
	$('#plate_userName').combobox({
		url:context+'/manager/bbs!plateOwnerData.action',
		valueField:'id',
		textField:'text'
	});
	if(userId!='')
		$('#plate_userName').combobox('select',userId);
}

Bbsplate.addPlate = function(){
	if(!util.checkFormFields($('#plate_form'))){
		alert("请按提示输入合法内容，或完成必填项！");
		return false;
	}
	$.ajax({
		type:"POST",
		url:context+'/manager/bbs!savePlate.action', 
		data:{
			'plateId':$('#plateId').val(),
			'title':$('#plate_title').val(),
			'userId':$("[name='plate_userName']").val(),
			'description':$('#plate_description').val()
		},
		success:function(text){
			if($('#plateId').val()==''){
				$.messager.defaults={ok:"继续添加",cancel:"返回板块列表"};
				$.messager.confirm('', '添加板块完成！您可以：', function(r){
					if (r){
						location.href = context+'/manager/bbs!addPlate.action';
					}else{
						location.href = context+'/manager/bbs.action?type=0';
					}
				});
			}else{
				$.messager.defaults={ok:"重新编辑",cancel:"返回板块列表"};
				$.messager.confirm('', '编辑板块完成！您可以：', function(r){
					if (!r){
						location.href = context+'/manager/bbs.action?type=0';
					}
				});
			}
		}
	})
}

var Bbstopic = {};

Bbstopic.init = function(){
	Bbstopic.initGrid();
}

Bbstopic.initGrid = function(){
	$('#topicListTable').datagrid({
		width:768,
		height:'auto',
		striped: true,
		url:context+'/manager/bbs!topicData.action',
		remoteSort: false,
		idField:'id',
		columns:[[
					{field:'title',title:'主题',width:110,align:'center'},
					{field:'authorName',title:'发贴人',width:110,align:'center'},
					{field:'hits',title:'浏览量',width:60,align:'center'},
					{field:'review',title:'回复量',width:60,align:'center'},
					{field:'plateName',title:'所属板块',width:110,align:'center'},
					{field:'addDate',title:'发贴时间',width:110,align:'center'},
					{field:'id',title:'操作',width:173,align:'center',formatter:function(value,rec){
						return "<a href='#' onclick='Bbstopic.openTopicDialog(\""+rec.content+"\")' style='color:blue'>查看内容</a> | <a href='"+context+"/manager/bbs!reviewTopic.action?topicId="+rec.id+"' onclick='#'  style='color:blue'>回贴管理</a> | <a href='#' onclick='Bbstopic.deleteTopic("+rec.id+")'  style='color:blue'>删除</a>";
					}}
				]],
		pagination:true,
		loadMsg:'正在加载数据,请等待...',
		rownumbers:true
	});
	var p = $('#topicListTable').datagrid('getPager');
	if (p){
		$(p).pagination({
			showPageList:false,
			beforePageText:'第',
			afterPageText:"页，共<font color='blue'> {pages} </font>页",
			displayMsg:"当前显示 <font color='red'>{from}</font> 到 <font color='red'>{to}</font> 条记录，共<font color='red'> {total} </font>条记录",
		});
	}
}

//查看贴子详细内容
Bbstopic.openTopicDialog= function(content){
	$('#topic_dialog').window({   
		modal:true,
		width:480,
		iconCls:'icon-ok',
		minimizable:false,     		
	}); 
	$('#topic_dialog').window('open');
	$('#topic_span').text(content);
}

Bbstopic.closeTopicDialog=function(){
	$('#topic_dialog').window('close');
}
//删除贴子
Bbstopic.deleteTopic=function(topicId){
	var is = confirm("确认删除！");
	if(is){
		$.ajax({
			type:'POST',
			url:context+'/manager/bbs!deleteReviewTopic.action',
			data:{'topicId':topicId},
			success:function(data){
				alert(data);
				Bbstopic.init();
			}
		})
	}
}


//回贴查询
Bbstopic.reviewTopicInit = function(){
	var topicId= $(":hidden[name=topicId]").val();
	$('#reviewTopicListTable').datagrid({
		width:768,
		height:'auto',
		striped: true,
		url:context+'/manager/bbs!reviewTopicData.action?topicId='+topicId,
		remoteSort: false,
		idField:'id',
		columns:[[
					{field:'title',title:'主题',width:190,align:'center'},
					{field:'authorName',title:'回贴人',width:190,align:'center'},
					{field:'addDate',title:'回贴时间',width:190,align:'center'},
					{field:'arrt',title:'操作',width:166,align:'center',formatter:function(value,rec){
						return "<a href='#' onclick='Bbstopic.openReviewTopicDialog(\""+rec.content+"\")' style='color:blue'>查看内容</a> | <a href='#' onclick='Bbstopic.deleteReviewTopic("+rec.id+")'  style='color:blue'>删除</a>";
					}}
				]],
		pagination:true,
		loadMsg:'正在加载数据,请等待...',
		rownumbers:true
	});
	var p = $('#reviewTopicListTable').datagrid('getPager');
	if (p){
		$(p).pagination({
			showPageList:false,
			beforePageText:'第',
			afterPageText:"页，共<font color='blue'> {pages} </font>页",
			displayMsg:"当前显示 <font color='red'>{from}</font> 到 <font color='red'>{to}</font> 条记录，共<font color='red'> {total} </font>条记录",
		});
	}
}

//查看回复贴子详细内容
Bbstopic.openReviewTopicDialog= function(content){
	$('#review_topic_dialog').window({   
		modal:true,
		width:480,
		iconCls:'icon-ok',
		minimizable:false,     		
	}); 
	$('#review_topic_dialog').window('open');
	$('#review_topic_span').text(content);
}

Bbstopic.closeReviewTopicDialog=function(){
	$('#review_topic_dialog').window('close');
}
//删除回复贴子
Bbstopic.deleteReviewTopic=function(topicId){
	var is = confirm("确认删除！");
	if(is){
		$.ajax({
			type:'POST',
			url:context+'/manager/bbs!deleteReviewTopic.action',
			data:{'topicId':topicId},
			success:function(data){
				alert(data);
				Bbstopic.reviewTopicInit(); 
			}
		})
	}
}


var Bbspoint = {};

Bbspoint.savePoint = function(){
	if(!util.checkFormFields($('#point_form'))){
		alert("请按提示输入合法内容，或完成必填项！");
		return false;
	}
	$.ajax({
		type:"POST",
		url:context+'/manager/bbs!savePoint.action?'+$('#point_form').serialize(), 
		data:{
			
		},
		success:function(text){
			$.messager.show({
				title:'消息',
				msg:"<h3>"+text+"</h3>",
				showType:'show'
			});
		}
	})
}

var PlateUserRelations={};
PlateUserRelations.init=function(type){
	if(type==0){
		$('#plateUserRelationsList').datagrid({
			width:768,
			height:'auto',
			striped: true,
			url:context+'/manager/plate-user-relations!getPlaterUserRelations.action?type='+type,
			remoteSort: false,
			idField:'id',
			columns:[[
						{field:'plateName',title:'板块',width:247,align:'center'},
						{field:'userName',title:'申请人',width:250,align:'center'},
						{field:'id',title:'操作',width:240,align:'center',formatter:function(value,rec){
							return "<a href='#' onclick='PlateUserRelations.comfirmed("+rec.id+","+rec.type+")' style='color:blue'>同意</a> | <a href='#' onclick='PlateUserRelations.deletep("+rec.id+","+rec.type+")'   style='color:blue'>删除</a>";
						}}
					]],
			pagination:true,
			loadMsg:'正在加载数据,请等待...',
			rownumbers:true
		});
	}else if(type ==1) {
		$('#plateUserRelationsList').datagrid({
			width:768,
			height:'auto',
			striped: true,
			url:context+'/manager/plate-user-relations!getPlaterUserRelations.action?type='+type,
			remoteSort: false,
			idField:'id',
			columns:[[
						{field:'plateName',title:'板块',width:247,align:'center'},
						{field:'userName',title:'版主',width:250,align:'center'},
						{field:'id',title:'操作',width:240,align:'center',formatter:function(value,rec){
							return "<a href='#' onclick='PlateUserRelations.deletep("+rec.id+","+rec.type+")'  style='color:blue'>删除</a>";
						}}
					]],
			pagination:true,
			loadMsg:'正在加载数据,请等待...',
			rownumbers:true
		});
	}
	
	var p = $('#plateUserRelationsList').datagrid('getPager');
	if (p){
		$(p).pagination({
			showPageList:false,
			beforePageText:'第',
			afterPageText:"页，共<font color='blue'> {pages} </font>页",
			displayMsg:"当前显示 <font color='red'>{from}</font> 到 <font color='red'>{to}</font> 条记录，共<font color='red'> {total} </font>条记录",
		});
	}
}
/**
 * 同意申请版主
 */
PlateUserRelations.comfirmed=function(id,type){
	$.messager.defaults={ok:"确定",cancel:"取消"};
	$.messager.confirm('', '确定同意该申请，同意后该申请人将会成为版主', function(r){
		if (r){
			$.ajax({
				type:"POST",
				url:context+'/manager/plate-user-relations!comfirmed.action', 
				data:{
					'id':id
				},
				success:function(data){
					alert(data);
					PlateUserRelations.init(type);
				}
			});
		}
	});
}

/**
 * 拒绝申请版主+删除已经存在的版主信息
 */
PlateUserRelations.deletep=function(id,type){
	$.messager.defaults={ok:"确定",cancel:"取消"};
	$.messager.confirm('', '确定删除？', function(r){
		if (r){
			$.ajax({
				type:"POST",
				url:context+'/manager/plate-user-relations!delete.action', 
				data:{
					'id':id
				},
				success:function(data){
					alert("删除成功");
					PlateUserRelations.init(type);
				}
			});
		}
	});
}
